/**
 * Dear programmer:
 * When I wrote this code, only god and I knew how it worked.
 * Now, only god knows it!
 *
 * @author: Pegasus  Date: 2018-11-26 Time: 22:12
 *
 * 下班
 */

import React, {Component} from 'react';
import {
  View,
  Image,
  Text,
  TouchableOpacity
} from 'react-native';
import {connect} from 'react-redux';
import {COLOR_WHITE,FONT_COLOR_TITLE} from "../../constant/PHColors"
import User from "../../model/User/User"
import {NavigationActions, StackActions} from "react-navigation"
import {offWork} from "../../screen/login/LoginScreen/Actions";
import {FetchStatus} from "../../configuration/RequestConfiguration";
import {getWs} from "../../configuration/global";

class OffWorkView extends Component {

    componentWillReceiveProps(nextProps, nextContext): void {
        let {dispatch} = this.props;

        if (nextProps.onOffWork !== this.props.onOffWork && nextProps.onOffWork.fetchStatus === FetchStatus.Success) {
            if (nextProps.onOffWork.RETURN === '10') {

            } else {

            }
        }
    }

  render() {
    let {dispatch} = this.props;

    return (
      <TouchableOpacity activeOpacity={0.9} onPress={() => {
          User.getUser(user => {
              let str = {"arg":{"userid":user.userid}};
              dispatch(offWork(JSON.stringify(str)));
          });


        console.log("--------下班--------")
        User.clear()

        if(getWs()!=null)
            getWs()._close();
        let resetAction = StackActions.reset({
          index: 0,
          actions: [
            NavigationActions.navigate({routeName: 'LoginScreen'})
          ]
        })
        this.props.navigation.dispatch(resetAction)
      }}>
        <View style={{width: 80, height: 44, flexDirection: 'row', justifyContent: 'center', alignItems: 'center'}}>
          <Text style={{fontSize: 16, color: FONT_COLOR_TITLE}}>
            下班
          </Text>
          <Image style={{width: 17, height: 17, marginLeft: 4}} source={require('../../resource/login/xiaban.png')}/>
        </View>
      </TouchableOpacity>
    );
  }
}

const props = state => {
  return {
      onOffWork: state.onOffWork,
  }
}

export default OffWorkView = connect(props)(OffWorkView)